home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / comp-pas.zip / TUTORPAS.ZIP / TUTOR5.DOC < prev    next >
Text File  |  1988-08-20  |  53KB  |  1,769 lines

  1. OPA A
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.                             LET'S BUILD A COMPILER!
  29.  
  30.                                        By
  31.  
  32.                             Jack W. Crenshaw, Ph.D.
  33.  
  34.                                  19 August 1988
  35.  
  36.  
  37.                            Part V: CONTROL CONSTRUCTS
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. PA A
  68.  
  69.  
  70.  
  71.  
  72.  
  73.        *****************************************************************
  74.        *                                                               *
  75.        *                        COPYRIGHT NOTICE                       *
  76.        *                                                               *
  77.        *   Copyright (C) 1988 Jack W. Crenshaw. All rights reserved.   *
  78.        *                                                               *
  79.        *****************************************************************
  80.  
  81.  
  82.        INTRODUCTION
  83.  
  84.        In  the  first  four  installments  of  this  series, we've  been
  85.        concentrating on the parsing of math  expressions  and assignment
  86.        statements.  In  this  installment,  we'll  take off on a new and
  87.        exciting  tangent:  that   of  parsing  and  translating  control
  88.        constructs such as IF statements.
  89.  
  90.        This subject is dear to my heart, because it represents a turning
  91.        point  for  me.    I  had  been  playing  with  the   parsing  of
  92.        expressions, just as  we  have  done  in this series, but I still
  93.        felt that I was a LONG way from being able  to  handle a complete
  94.        language.  After all, REAL  languages have branches and loops and
  95.        subroutines and all that.  Perhaps you've shared some of the same
  96.        thoughts.    Awhile  back,  though,  I  had  to  produce  control
  97.        constructs for a structured assembler preprocessor I was writing.
  98.        Imagine my surprise to  discover  that it was far easier than the
  99.        expression  parsing  I  had  already  been through.   I  remember
  100.        thinking, "Hey! This is EASY!" After we've finished this session,
  101.        I'll bet you'll be thinking so, too.
  102.  
  103.  
  104.        THE PLAN
  105.  
  106.        In what follows, we'll be starting over again with a bare cradle,
  107.        and as we've done twice before now, we'll build things up  one at
  108.        a time.  We'll also  be retaining the concept of single-character
  109.        tokens that has served us so well to date.   This  means that the
  110.        "code" will look a little funny, with 'i' for IF, 'w'  for WHILE,
  111.        etc.  But it helps us  get  the concepts down pat without fussing
  112.        over  lexical  scanning.    Fear  not  ...  eventually we'll  see
  113.        something looking like "real" code.
  114.  
  115.        I also don't  want  to  have  us  get bogged down in dealing with
  116.        statements other than branches, such as the assignment statements
  117.        we've  been  working  on.  We've already demonstrated that we can
  118.        handle them, so there's no point carrying them  around  as excess
  119.        baggage during this exercise.  So what I'll do instead is  to use
  120.        an  anonymous  statement,  "other", to take the place of the non-
  121.        control statements and serve as a place-holder for them.  We have
  122.        to generate some kind of object code for them  (we're  back  into
  123.        compiling, not interpretation), so for want of anything else I'll
  124.        just echo the character input.ABAB
  125.                                      - 2 -A*A*
  126. PA A
  127.  
  128.  
  129.  
  130.  
  131.  
  132.        OK, then, starting with  yet  another  copy  of the cradle, let's
  133.        define the procedure:
  134.  
  135.  
  136.        {--------------------------------------------------------------}
  137.        { Recognize and Translate an "Other" }
  138.  
  139.        procedure Other;
  140.        begin
  141.           EmitLn(GetName);
  142.        end;
  143.        {--------------------------------------------------------------}
  144.  
  145.  
  146.        Now include a call to it in the main program, thus:
  147.  
  148.  
  149.        {--------------------------------------------------------------}
  150.        { Main Program }
  151.  
  152.        begin
  153.           Init;
  154.           Other;
  155.        end.
  156.        {--------------------------------------------------------------}
  157.  
  158.  
  159.        Run  the program and see what you get.  Not very exciting, is it?
  160.        But hang in there, it's a start, and things will get better.
  161.  
  162.        The first thing we need is the ability to deal with more than one
  163.        statement, since a single-line branch  is pretty limited.  We did
  164.        that in the last session on interpreting, but this time let's get
  165.        a little more formal.  Consider the following BNF:
  166.  
  167.                  <program> ::= <block> END
  168.  
  169.                  <block> ::= [ <statement> ]*
  170.  
  171.        This says that, for our purposes here, a program is defined  as a
  172.        block, followed by an END statement.  A block, in  turn, consists
  173.        of zero or more statements.  We only have one kind  of statement,
  174.        so far.
  175.  
  176.        What signals the end of a block?  It's  simply any construct that
  177.        isn't an "other"  statement.    For  now, that means only the END
  178.        statement.
  179.  
  180.        Armed with these ideas, we can proceed to build  up  our  parser.
  181.        The code for a program (we  have  to call it DoProgram, or Pascal
  182.        will complain, is:ANAN
  183.                                      - 3 -A*A*
  184. PA A
  185.  
  186.  
  187.  
  188.  
  189.  
  190.        {--------------------------------------------------------------}
  191.        { Parse and Translate a Program }
  192.  
  193.        procedure DoProgram;
  194.        begin
  195.           Block;
  196.           if Look <> 'e' then Expected('End');
  197.           EmitLn('END')
  198.        end;
  199.        {--------------------------------------------------------------}
  200.  
  201.  
  202.        Notice  that  I've  arranged to emit  an  "END"  command  to  the
  203.        assembler, which sort of  punctuates  the  output code, and makes
  204.        sense considering that we're parsing a complete program here.
  205.  
  206.        The code for Block is:
  207.  
  208.  
  209.        {--------------------------------------------------------------}
  210.        { Recognize and Translate a Statement Block }
  211.  
  212.        procedure Block;
  213.        begin
  214.           while not(Look in ['e']) do begin
  215.              Other;
  216.           end;
  217.        end;
  218.        {--------------------------------------------------------------}
  219.  
  220.  
  221.        (From the form of the procedure, you just KNOW we're going  to be
  222.        adding to it in a bit!)
  223.  
  224.        OK, enter these routines into your program.  Replace the  call to
  225.        Block in the main program, by  a  call  to DoProgram.  Now try it
  226.        and  see  how  it works.  Well, it's still not  much,  but  we're
  227.        getting closer.
  228.  
  229.  
  230.        SOME GROUNDWORK
  231.  
  232.        Before we begin to define the various control constructs, we need
  233.        to  lay a bit more groundwork.  First, a word of warning: I won't
  234.        be using the same syntax  for these constructs as you're familiar
  235.        with  from Pascal or C.  For example, the Pascal syntax for an IF
  236.        is:
  237.  
  238.  
  239.             IF <condition> THEN <statement>
  240.  
  241.  
  242.        (where the statement, of course, may be compound).A6A6
  243.                                      - 4 -A*A*
  244. PA A
  245.  
  246.  
  247.  
  248.  
  249.  
  250.        The C version is similar:
  251.  
  252.  
  253.             IF ( <condition> ) <statement>
  254.  
  255.  
  256.        Instead, I'll be using something that looks more like Ada:
  257.  
  258.  
  259.             IF <condition> <block> ENDIF
  260.  
  261.  
  262.        In  other  words,  the IF construct has  a  specific  termination
  263.        symbol.  This avoids  the  dangling-else of Pascal and C and also
  264.        precludes the need for the brackets {} or begin-end.   The syntax
  265.        I'm showing you here, in fact, is that of the language  KISS that
  266.        I'll be detailing in  later  installments.   The other constructs
  267.        will also be  slightly  different.    That  shouldn't  be  a real
  268.        problem for you.  Once you see how it's done, you'll realize that
  269.        it  really  doesn't  matter  so  much  which  specific syntax  is
  270.        involved.  Once the syntax is defined, turning it  into  code  is
  271.        straightforward.
  272.  
  273.        Now, all of the  constructs  we'll  be  dealing with here involve
  274.        transfer of control, which at the assembler-language  level means
  275.        conditional  and/or  unconditional branches.   For  example,  the
  276.        simple IF statement
  277.  
  278.  
  279.                  IF <condition> A ENDIF B ....
  280.  
  281.        must get translated into
  282.  
  283.                  Branch if NOT condition to L
  284.                  A
  285.             L:   B
  286.                  ...
  287.  
  288.  
  289.        It's clear, then, that we're going to need  some  more procedures
  290.        to  help  us  deal with these branches.  I've defined two of them
  291.        below.  Procedure NewLabel generates unique labels.  This is done
  292.        via the simple expedient of calling every label  'Lnn',  where nn
  293.        is a label number starting from zero.   Procedure  PostLabel just
  294.        outputs the labels at the proper place.
  295.  
  296.        Here are the two routines:A?A?
  297.  
  298.                                      - 5 -A*A*
  299. PA A
  300.  
  301.  
  302.  
  303.  
  304.  
  305.        {--------------------------------------------------------------}
  306.        { Generate a Unique Label }
  307.  
  308.        function NewLabel: string;
  309.        var S: string;
  310.        begin
  311.           Str(LCount, S);
  312.           NewLabel := 'L' + S;
  313.           Inc(LCount);
  314.        end;
  315.  
  316.  
  317.        {--------------------------------------------------------------}
  318.        { Post a Label To Output }
  319.  
  320.        procedure PostLabel(L: string);
  321.        begin
  322.           WriteLn(L, ':');
  323.        end;
  324.        {--------------------------------------------------------------}
  325.  
  326.  
  327.        Notice that we've added  a  new  global  variable, LCount, so you
  328.        need to change the VAR declarations at the top of the  program to
  329.        look like this:
  330.  
  331.  
  332.        var Look  : char;              { Lookahead Character }
  333.            Lcount: integer;           { Label Counter }
  334.  
  335.  
  336.        Also, add the following extra initialization to Init:
  337.  
  338.  
  339.           LCount := 0;
  340.  
  341.        (DON'T forget that, or your labels can look really strange!)
  342.  
  343.  
  344.        At this point I'd also like to show you a  new  kind of notation.
  345.        If  you  compare  the form of the IF statement above with the as-
  346.        sembler code that must be produced, you can see  that  there  are
  347.        certain  actions  associated  with each of the  keywords  in  the
  348.        statement:
  349.  
  350.  
  351.             IF:  First, get the condition and issue the code for it.
  352.                  Then, create a unique label and emit a branch if false.
  353.  
  354.             ENDIF: Emit the label.
  355.  
  356.  
  357.        These actions can be shown very concisely if we write  the syntax
  358.        this way:A*A*
  359.                                      - 6 -
  360. PA A
  361.  
  362.  
  363.  
  364.  
  365.  
  366.             IF
  367.             <condition>    { Condition;
  368.                              L = NewLabel;
  369.                              Emit(Branch False to L); }
  370.             <block>
  371.             ENDIF          { PostLabel(L) }
  372.  
  373.  
  374.        This is an example  of  syntax-directed  translation.  We've been
  375.        doing it all along ... we've just never written it down  this way
  376.        before.  The stuff in curly brackets represents the ACTIONS to be
  377.        taken.  The nice part about this representation is  that  it  not
  378.        only shows what  we  have  to  recognize, but also the actions we
  379.        have to perform, and in which  order.   Once we have this syntax,
  380.        the code almost writes itself.
  381.  
  382.        About  the  only thing left to do is to be a  bit  more  specific
  383.        about what we mean by "Branch if false."
  384.  
  385.        I'm assuming that there will  be  code  executed  for <condition>
  386.        that  will  perform  Boolean algebra and compute some result.  It
  387.        should also set the condition flags corresponding to that result.
  388.        Now, the usual convention  for  a Boolean variable is to let 0000
  389.        represent "false," and  anything  else (some use FFFF, some 0001)
  390.        represent "true."
  391.  
  392.        On the 68000  the  condition  flags  are set whenever any data is
  393.        moved or calculated.  If the  data  is a 0000 (corresponding to a
  394.        false condition, remember), the zero flag will be set.   The code
  395.        for "Branch on zero" is BEQ.  So for our purposes here,
  396.  
  397.  
  398.                       BEQ  <=> Branch if false
  399.                       BNE  <=> Branch if true
  400.  
  401.  
  402.        It's the nature of the beast that most  of  the  branches  we see
  403.        will  be  BEQ's  ...  we'll  be branching AROUND the code  that's
  404.        supposed to be executed when the condition is true.
  405.  
  406.  
  407.        THE IF STATEMENT
  408.  
  409.        With that bit of explanation out of the way, we're  finally ready
  410.        to begin coding the IF-statement parser.  In  fact,  we've almost
  411.        already  done  it!   As usual, I'll be using our single-character
  412.        approach, with the character 'i' for IF, and 'e'  for  ENDIF  (as
  413.        well  as END ... that dual nature causes  no  confusion).    I'll
  414.        also, for now, skip completely  the character for the branch con-
  415.        dition, which we still have to define.
  416.  
  417.        The code for DoIf is:ABAB
  418.                                      - 7 -A*A*
  419. PA A
  420.  
  421.  
  422.  
  423.  
  424.  
  425.        {--------------------------------------------------------------}
  426.        { Recognize and Translate an IF Construct }
  427.  
  428.        procedure Block; Forward;
  429.  
  430.  
  431.        procedure DoIf;
  432.        var L: string;
  433.        begin
  434.           Match('i');
  435.           L := NewLabel;
  436.           Condition;
  437.           EmitLn('BEQ ' + L);
  438.           Block;
  439.           Match('e');
  440.           PostLabel(L);
  441.        end;
  442.        {--------------------------------------------------------------}
  443.  
  444.  
  445.        Add this routine to your program, and change  Block  to reference
  446.        it as follows:
  447.  
  448.  
  449.        {--------------------------------------------------------------}
  450.        { Recognize and Translate a Statement Block }
  451.  
  452.        procedure Block;
  453.        begin
  454.           while not(Look in ['e']) do begin
  455.              case Look of
  456.               'i': DoIf;
  457.               'o': Other;
  458.              end;
  459.           end;
  460.        end;
  461.        {--------------------------------------------------------------}
  462.  
  463.  
  464.        Notice the reference to procedure Condition.    Eventually, we'll
  465.        write a routine that  can  parse  and  translate any Boolean con-
  466.        dition we care to give it.  But  that's  a  whole  installment by
  467.        itself (the next one, in fact).    For  now, let's just make it a
  468.        dummy that emits some text.  Write the following routine:AQAQ
  469.  
  470.                                      - 8 -A*A*
  471. PA A
  472.  
  473.  
  474.  
  475.  
  476.  
  477.        {--------------------------------------------------------------}
  478.        { Parse and Translate a Boolean Condition }
  479.        { This version is a dummy }
  480.  
  481.        Procedure Condition;
  482.        begin
  483.           EmitLn('<condition>');
  484.        end;
  485.        {--------------------------------------------------------------}
  486.  
  487.  
  488.        Insert this procedure in your program just before DoIf.   Now run
  489.        the program.  Try a string like
  490.  
  491.             aibece
  492.  
  493.        As you can see,  the  parser seems to recognize the construct and
  494.        inserts the object code at the  right  places.   Now try a set of
  495.        nested IF's, like
  496.  
  497.             aibicedefe
  498.  
  499.        It's starting to look real, eh?
  500.  
  501.        Now that we  have  the  general  idea  (and the tools such as the
  502.        notation and the procedures NewLabel and PostLabel), it's a piece
  503.        of cake to extend the parser to include other  constructs.    The
  504.        first (and also one of the  trickiest)  is to add the ELSE clause
  505.        to IF.  The BNF is
  506.  
  507.  
  508.             IF <condition> <block> [ ELSE <block>] ENDIF
  509.  
  510.  
  511.        The tricky part arises simply  because there is an optional part,
  512.        which doesn't occur in the other constructs.
  513.  
  514.        The corresponding output code should be
  515.  
  516.  
  517.                  <condition>
  518.                  BEQ L1
  519.                  <block>
  520.                  BRA L2
  521.             L1:  <block>
  522.             L2:  ...
  523.  
  524.  
  525.        This leads us to the following syntax-directed translation:A3A3
  526.  
  527.                                      - 9 -A*A*
  528. PA A
  529.  
  530.  
  531.  
  532.  
  533.  
  534.             IF
  535.             <condition>    { L1 = NewLabel;
  536.                              L2 = NewLabel;
  537.                              Emit(BEQ L1) }
  538.             <block>
  539.             ELSE           { Emit(BRA L2);
  540.                              PostLabel(L1) }
  541.             <block>
  542.             ENDIF          { PostLabel(L2) }
  543.  
  544.  
  545.        Comparing this with the case for an ELSE-less IF gives us  a clue
  546.        as to how to handle both situations.   The  code  below  does it.
  547.        (Note that I  use  an  'l'  for  the ELSE, since 'e' is otherwise
  548.        occupied):
  549.  
  550.  
  551.        {--------------------------------------------------------------}
  552.        { Recognize and Translate an IF Construct }
  553.  
  554.        procedure DoIf;
  555.        var L1, L2: string;
  556.        begin
  557.           Match('i');
  558.           Condition;
  559.           L1 := NewLabel;
  560.           L2 := L1;
  561.           EmitLn('BEQ ' + L1);
  562.           Block;
  563.           if Look = 'l' then begin
  564.              Match('l');
  565.              L2 := NewLabel;
  566.              EmitLn('BRA ' + L2);
  567.              PostLabel(L1);
  568.              Block;
  569.           end;
  570.           Match('e');
  571.           PostLabel(L2);
  572.        end;
  573.        {--------------------------------------------------------------}
  574.  
  575.  
  576.        There you have it.  A complete IF parser/translator, in  19 lines
  577.        of code.
  578.  
  579.        Give it a try now.  Try something like
  580.  
  581.           aiblcede
  582.  
  583.        Did it work?  Now, just  to  be  sure we haven't broken the ELSE-
  584.        less case, try
  585.  
  586.           aibeceA6A6
  587.                                     - 10 -A*A*
  588. PA A
  589.  
  590.  
  591.  
  592.  
  593.  
  594.        Now try some nested IF's.  Try anything you like,  including some
  595.        badly formed statements.   Just  remember that 'e' is not a legal
  596.        "other" statement.
  597.  
  598.  
  599.        THE WHILE STATEMENT
  600.  
  601.        The next type of statement should be easy, since we  already have
  602.        the process  down  pat.    The  syntax  I've chosen for the WHILE
  603.        statement is
  604.  
  605.  
  606.                  WHILE <condition> <block> ENDWHILE
  607.  
  608.  
  609.        I know,  I  know,  we  don't  REALLY  need separate kinds of ter-
  610.        minators for each construct ... you can see that by the fact that
  611.        in our one-character version, 'e' is used for all of them.  But I
  612.        also remember  MANY debugging sessions in Pascal, trying to track
  613.        down a wayward END that the compiler obviously thought I meant to
  614.        put  somewhere  else.   It's been my experience that specific and
  615.        unique  keywords,  although  they add to the  vocabulary  of  the
  616.        language,  give  a  bit of error-checking that is worth the extra
  617.        work for the compiler writer.
  618.  
  619.        Now,  consider  what  the  WHILE  should be translated into.   It
  620.        should be:
  621.  
  622.  
  623.             L1:  <condition>
  624.                  BEQ L2
  625.                  <block>
  626.                  BRA L1
  627.             L2:
  628.  
  629.  
  630.        As before, comparing the two representations gives us the actions
  631.        needed at each point.
  632.  
  633.  
  634.             WHILE          { L1 = NewLabel;
  635.                              PostLabel(L1) }
  636.             <condition>    { Emit(BEQ L2) }
  637.             <block>
  638.             ENDWHILE       { Emit(BRA L1);
  639.                              PostLabel(L2) }
  640.  
  641.  
  642.        The code follows immediately from the syntax:A3A3
  643.  
  644.                                     - 11 -A*A*
  645. PA A
  646.  
  647.  
  648.  
  649.  
  650.  
  651.        {--------------------------------------------------------------}
  652.        { Parse and Translate a WHILE Statement }
  653.  
  654.        procedure DoWhile;
  655.        var L1, L2: string;
  656.        begin
  657.           Match('w');
  658.           L1 := NewLabel;
  659.           L2 := NewLabel;
  660.           PostLabel(L1);
  661.           Condition;
  662.           EmitLn('BEQ ' + L2);
  663.           Block;
  664.           Match('e');
  665.           EmitLn('BRA ' + L1);
  666.           PostLabel(L2);
  667.        end;
  668.        {--------------------------------------------------------------}
  669.  
  670.  
  671.        Since  we've  got a new statement, we have to add a  call  to  it
  672.        within procedure Block:
  673.  
  674.  
  675.        {--------------------------------------------------------------}
  676.        { Recognize and Translate a Statement Block }
  677.  
  678.        procedure Block;
  679.        begin
  680.           while not(Look in ['e', 'l']) do begin
  681.              case Look of
  682.               'i': DoIf;
  683.               'w': DoWhile;
  684.               else Other;
  685.              end;
  686.           end;
  687.        end;
  688.        {--------------------------------------------------------------}
  689.  
  690.  
  691.        No other changes are necessary.
  692.  
  693.        OK, try the new program.  Note that this  time,  the  <condition>
  694.        code is INSIDE the upper label, which is just where we wanted it.
  695.        Try some nested loops.  Try some loops within IF's, and some IF's
  696.        within loops.  If you get  a  bit  confused as to what you should
  697.        type, don't be discouraged:  you  write  bugs in other languages,
  698.        too, don't you?  It'll look a lot  more  meaningful  when  we get
  699.        full keywords.
  700.  
  701.        I hope by now that you're beginning to  get  the  idea  that this
  702.        really  IS easy.  All we have to do to accomodate a new construct
  703.        is to work out  the  syntax-directed translation of it.  The code
  704.        almost falls out  from  there,  and  it doesn't affect any of theA*A*
  705.                                     - 12 -
  706. PA A
  707.  
  708.  
  709.  
  710.  
  711.  
  712.        other routines.  Once you've gotten the feel of the thing, you'll
  713.        see that you  can  add  new  constructs  about as fast as you can
  714.        dream them up.
  715.  
  716.  
  717.        THE LOOP STATEMENT
  718.  
  719.        We could stop right here, and  have  a language that works.  It's
  720.        been  shown  many  times that a high-order language with only two
  721.        constructs, the IF and the WHILE, is sufficient  to  write struc-
  722.        tured  code.   But we're on a roll now, so let's  richen  up  the
  723.        repertoire a bit.
  724.  
  725.        This construct is even easier, since it has no condition  test at
  726.        all  ... it's an infinite loop.  What's the point of such a loop?
  727.        Not much, by  itself,  but  later  on  we're going to add a BREAK
  728.        command,  that  will  give us a way out.  This makes the language
  729.        considerably richer than Pascal, which  has  no  break,  and also
  730.        avoids the funny  WHILE(1) or WHILE TRUE of C and Pascal.
  731.  
  732.        The syntax is simply
  733.  
  734.             LOOP <block> ENDLOOP
  735.  
  736.        and the syntax-directed translation is:
  737.  
  738.  
  739.             LOOP           { L = NewLabel;
  740.                              PostLabel(L) }
  741.             <block>
  742.             ENDLOOP        { Emit(BRA L }
  743.  
  744.  
  745.        The corresponding code is shown below.  Since  I've  already used
  746.        'l'  for  the  ELSE, I've used  the  last  letter,  'p',  as  the
  747.        "keyword" this time.
  748.  
  749.  
  750.        {--------------------------------------------------------------}
  751.        { Parse and Translate a LOOP Statement }
  752.  
  753.        procedure DoLoop;
  754.        var L: string;
  755.        begin
  756.           Match('p');
  757.           L := NewLabel;
  758.           PostLabel(L);
  759.           Block;
  760.           Match('e');
  761.           EmitLn('BRA ' + L);
  762.        end;
  763.        {--------------------------------------------------------------}ABAB
  764.                                     - 13 -A*A*
  765. PA A
  766.  
  767.  
  768.  
  769.  
  770.  
  771.        When you insert this routine, don't forget to add a line in Block
  772.        to call it.
  773.  
  774.  
  775.        REPEAT-UNTIL
  776.  
  777.        Here's one construct that I lifted right from Pascal.  The syntax
  778.        is
  779.  
  780.  
  781.             REPEAT <block> UNTIL <condition>  ,
  782.  
  783.  
  784.        and the syntax-directed translation is:
  785.  
  786.  
  787.             REPEAT         { L = NewLabel;
  788.                              PostLabel(L) }
  789.             <block>
  790.             UNTIL
  791.             <condition>    { Emit(BEQ L) }
  792.  
  793.  
  794.        As usual, the code falls out pretty easily:
  795.  
  796.  
  797.        {--------------------------------------------------------------}
  798.        { Parse and Translate a REPEAT Statement }
  799.  
  800.        procedure DoRepeat;
  801.        var L: string;
  802.        begin
  803.           Match('r');
  804.           L := NewLabel;
  805.           PostLabel(L);
  806.           Block;
  807.           Match('u');
  808.           Condition;
  809.           EmitLn('BEQ ' + L);
  810.        end;
  811.        {--------------------------------------------------------------}
  812.  
  813.  
  814.        As  before, we have to add the call  to  DoRepeat  within  Block.
  815.        This time, there's a difference, though.  I decided  to  use  'r'
  816.        for REPEAT (naturally), but I also decided to use 'u'  for UNTIL.
  817.        This means that the 'u' must be added to the set of characters in
  818.        the while-test.  These  are  the  characters  that signal an exit
  819.        from the current  block  ... the "follow" characters, in compiler
  820.        jargon.A-A-
  821.  
  822.                                     - 14 -A*A*
  823. PA A
  824.  
  825.  
  826.  
  827.  
  828.  
  829.        {--------------------------------------------------------------}
  830.        { Recognize and Translate a Statement Block }
  831.  
  832.        procedure Block;
  833.        begin
  834.           while not(Look in ['e', 'l', 'u']) do begin
  835.              case Look of
  836.               'i': DoIf;
  837.               'w': DoWhile;
  838.               'p': DoLoop;
  839.               'r': DoRepeat;
  840.               else Other;
  841.              end;
  842.           end;
  843.        end;
  844.        {--------------------------------------------------------------}
  845.  
  846.  
  847.        THE FOR LOOP
  848.  
  849.        The FOR loop  is a very handy one to have around, but it's a bear
  850.        to translate.  That's not so much because the construct itself is
  851.        hard ... it's only a loop  after  all ... but simply because it's
  852.        hard to implement  in  assembler  language.    Once  the  code is
  853.        figured out, the translation is straightforward enough.
  854.  
  855.        C fans love  the  FOR-loop  of  that language (and, in fact, it's
  856.        easier to code), but I've chosen instead a syntax very  much like
  857.        the one from good ol' BASIC:
  858.  
  859.  
  860.             FOR <ident> = <expr1> TO <expr2> <block> ENDFOR
  861.  
  862.  
  863.        The translation of a FOR loop  can  be just about as difficult as
  864.        you choose  to  make  it,  depending  upon  the way you decide to
  865.        define  the rules as to how to handle the limits.  Does expr2 get
  866.        evaluated  every time through the loop, for  example,  or  is  it
  867.        treated as a constant limit?   Do  you always go through the loop
  868.        at least once,  as  in  FORTRAN,  or  not? It gets simpler if you
  869.        adopt the point of view that the construct is equivalent to:
  870.  
  871.  
  872.             <ident> = <expr1>
  873.             TEMP = <expr2>
  874.             WHILE <ident> <= TEMP
  875.             <block>
  876.             ENDWHILE
  877.  
  878.  
  879.        Notice that with this definition of the loop, <block> will not be
  880.        executed at all if <expr1> is initially larger than <expr2>.ABAB
  881.                                     - 15 -A*A*
  882. PA A
  883.  
  884.  
  885.  
  886.  
  887.  
  888.        The 68000 code needed to do this is trickier than  anything we've
  889.        done so far.  I had a couple  of  tries  at  it, putting both the
  890.        counter  and  the    upper limit on the stack, both in registers,
  891.        etc.  I  finally  arrived  at  a hybrid arrangement, in which the
  892.        loop counter is in memory (so that it can be accessed  within the
  893.        loop), and the upper limit is on the stack.  The  translated code
  894.        came out like this:
  895.  
  896.  
  897.                  <ident>             get name of loop counter
  898.                  <expr1>             get initial value
  899.                  LEA <ident>(PC),A0  address the loop counter
  900.                  SUBQ #1,D0          predecrement it
  901.                  MOVE D0,(A0)        save it
  902.                  <expr1>             get upper limit
  903.                  MOVE D0,-(SP)       save it on stack
  904.  
  905.             L1:  LEA <ident>(PC),A0  address loop counter
  906.                  MOVE (A0),D0        fetch it to D0
  907.                  ADDQ #1,D0          bump the counter
  908.                  MOVE D0,(A0)        save new value
  909.                  CMP (SP),D0         check for range
  910.                  BLE L2              skip out if D0 > (SP)
  911.                  <block>
  912.                  BRA L1              loop for next pass
  913.             L2:  ADDQ #2,SP          clean up the stack
  914.  
  915.  
  916.        Wow!    That  seems like a lot of code ...  the  line  containing
  917.        <block> seems to almost get lost.  But that's the best I could do
  918.        with it.   I guess it helps to keep in mind that it's really only
  919.        sixteen  words,  after  all.  If  anyone else can  optimize  this
  920.        better, please let me know.
  921.  
  922.        Still, the parser  routine  is  pretty  easy now that we have the
  923.        code:
  924.  
  925.  
  926.        {--------------------------------------------------------------}
  927.        { Parse and Translate a FOR Statement }
  928.  
  929.        procedure DoFor;
  930.        var L1, L2: string;
  931.            Name: char;
  932.        begin
  933.           Match('f');
  934.           L1 := NewLabel;
  935.           L2 := NewLabel;
  936.           Name := GetName;
  937.           Match('=');
  938.           Expression;
  939.           EmitLn('SUBQ #1,D0');
  940.           EmitLn('LEA ' + Name + '(PC),A0');
  941.           EmitLn('MOVE D0,(A0)');A*A*
  942.                                     - 16 -
  943. PA A
  944.  
  945.  
  946.  
  947.  
  948.  
  949.           Expression;
  950.           EmitLn('MOVE D0,-(SP)');
  951.           PostLabel(L1);
  952.           EmitLn('LEA ' + Name + '(PC),A0');
  953.           EmitLn('MOVE (A0),D0');
  954.           EmitLn('ADDQ #1,D0');
  955.           EmitLn('MOVE D0,(A0)');
  956.           EmitLn('CMP (SP),D0');
  957.           EmitLn('BGT ' + L2);
  958.           Block;
  959.           Match('e');
  960.           EmitLn('BRA ' + L1);
  961.           PostLabel(L2);
  962.           EmitLn('ADDQ #2,SP');
  963.        end;
  964.        {--------------------------------------------------------------}
  965.  
  966.  
  967.        Since we don't have  expressions  in this parser, I used the same
  968.        trick as for Condition, and wrote the routine
  969.  
  970.  
  971.        {--------------------------------------------------------------}
  972.        { Parse and Translate an Expression }
  973.        { This version is a dummy }
  974.  
  975.        Procedure Expression;
  976.        begin
  977.           EmitLn('<expr>');
  978.        end;
  979.        {--------------------------------------------------------------}
  980.  
  981.  
  982.        Give it a try.  Once again,  don't  forget  to  add  the  call in
  983.        Block.    Since  we don't have any input for the dummy version of
  984.        Expression, a typical input line would look something like
  985.  
  986.             afi=bece
  987.  
  988.        Well, it DOES generate a lot of code, doesn't it?    But at least
  989.        it's the RIGHT code.
  990.  
  991.  
  992.        THE DO STATEMENT
  993.  
  994.        All this made me wish for a simpler version of the FOR loop.  The
  995.        reason for all the code  above  is  the  need  to  have  the loop
  996.        counter accessible as a variable within the loop.  If all we need
  997.        is a counting loop to make us go through  something  a  specified
  998.        number of times, but  don't  need  access  to the counter itself,
  999.        there is a much easier solution.  The 68000 has a  "decrement and
  1000.        branch nonzero" instruction built in which is ideal for counting.
  1001.        For good measure, let's add this construct, too.   This  will  be
  1002.        the last of our loop structures.A*A*
  1003.                                     - 17 -
  1004. PA A
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.        The syntax and its translation is:
  1011.  
  1012.  
  1013.             DO
  1014.             <expr>         { Emit(SUBQ #1,D0);
  1015.                              L = NewLabel;
  1016.                              PostLabel(L);
  1017.                              Emit(MOVE D0,-(SP) }
  1018.             <block>
  1019.             ENDDO          { Emit(MOVE (SP)+,D0;
  1020.                              Emit(DBRA D0,L) }
  1021.  
  1022.  
  1023.        That's quite a bit simpler!  The loop will execute  <expr> times.
  1024.        Here's the code:
  1025.  
  1026.  
  1027.        {--------------------------------------------------------------}
  1028.        { Parse and Translate a DO Statement }
  1029.  
  1030.        procedure Dodo;
  1031.        var L: string;
  1032.        begin
  1033.           Match('d');
  1034.           L := NewLabel;
  1035.           Expression;
  1036.           EmitLn('SUBQ #1,D0');
  1037.           PostLabel(L);
  1038.           EmitLn('MOVE D0,-(SP)');
  1039.           Block;
  1040.           EmitLn('MOVE (SP)+,D0');
  1041.           EmitLn('DBRA D0,' + L);
  1042.        end;
  1043.        {--------------------------------------------------------------}
  1044.  
  1045.  
  1046.        I think you'll have to agree, that's a whole lot simpler than the
  1047.        classical FOR.  Still, each construct has its place.
  1048.  
  1049.  
  1050.        THE BREAK STATEMENT
  1051.  
  1052.        Earlier I promised you a BREAK statement to accompany LOOP.  This
  1053.        is  one  I'm sort of proud of.  On the face of it a  BREAK  seems
  1054.        really  tricky.  My first approach was to just use it as an extra
  1055.        terminator to Block, and split all the loops into two parts, just
  1056.        as  I did with the ELSE half of an IF.  That  turns  out  not  to
  1057.        work, though, because the BREAK statement is almost certainly not
  1058.        going to show  up at the same level as the loop itself.  The most
  1059.        likely place for a BREAK is right after an IF, which  would cause
  1060.        it to exit to the IF  construct,  not the enclosing loop.  WRONG.
  1061.        The  BREAK  has  to exit the inner LOOP, even if it's nested down
  1062.        into several levels of IFs.A6A6
  1063.                                     - 18 -A*A*
  1064. PA A
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.        My next thought was that I would just store away, in  some global
  1071.        variable, the ending label of the innermost loop.    That doesn't
  1072.        work  either, because there may be a break  from  an  inner  loop
  1073.        followed by a break from an outer one.  Storing the label for the
  1074.        inner loop would clobber the label for the  outer  one.    So the
  1075.        global variable turned into a stack.  Things were starting to get
  1076.        messy.
  1077.  
  1078.        Then  I  decided  to take my own advice.  Remember  in  the  last
  1079.        session when  I  pointed  out  how  well  the implicit stack of a
  1080.        recursive descent parser was  serving  our needs?  I said that if
  1081.        you begin to  see  the  need  for  an external stack you might be
  1082.        doing  something  wrong.   Well, I was.  It is indeed possible to
  1083.        let the recursion built into  our parser take care of everything,
  1084.        and the solution is so simple that it's surprising.
  1085.  
  1086.        The secret is  to  note  that  every BREAK statement has to occur
  1087.        within a block ... there's no place else for it to be.  So all we
  1088.        have  to  do  is to pass into  Block  the  exit  address  of  the
  1089.        innermost loop.  Then it can pass the address to the routine that
  1090.        translates the  break instruction.  Since an IF statement doesn't
  1091.        change the loop level, procedure DoIf doesn't need to do anything
  1092.        except  pass the label into ITS blocks (both  of  them).    Since
  1093.        loops DO change the level,  each  loop  construct  simply ignores
  1094.        whatever label is above it and passes its own exit label along.
  1095.  
  1096.        All  this  is easier to show you than it is to  describe.    I'll
  1097.        demonstrate with the easiest loop, which is LOOP:
  1098.  
  1099.  
  1100.        {--------------------------------------------------------------}
  1101.        { Parse and Translate a LOOP Statement }
  1102.  
  1103.        procedure DoLoop;
  1104.        var L1, L2: string;
  1105.        begin
  1106.           Match('p');
  1107.           L1 := NewLabel;
  1108.           L2 := NewLabel;
  1109.           PostLabel(L1);
  1110.           Block(L2);
  1111.           Match('e');
  1112.           EmitLn('BRA ' + L1);
  1113.           PostLabel(L2);
  1114.        end;
  1115.        {--------------------------------------------------------------}
  1116.  
  1117.  
  1118.        Notice that DoLoop now has TWO labels, not just one.   The second
  1119.        is to give the BREAK instruction a target to jump  to.   If there
  1120.        is no BREAK within  the  loop, we've wasted a label and cluttered
  1121.        up things a bit, but there's no harm done.ABAB
  1122.                                     - 19 -A*A*
  1123. PA A
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.        Note also that Block now has a parameter, which  for  loops  will
  1130.        always be the exit address.  The new version of Block is:
  1131.  
  1132.  
  1133.        {--------------------------------------------------------------}
  1134.        { Recognize and Translate a Statement Block }
  1135.  
  1136.        procedure Block(L: string);
  1137.        begin
  1138.           while not(Look in ['e', 'l', 'u']) do begin
  1139.              case Look of
  1140.               'i': DoIf(L);
  1141.               'w': DoWhile;
  1142.               'p': DoLoop;
  1143.               'r': DoRepeat;
  1144.               'f': DoFor;
  1145.               'd': DoDo;
  1146.               'b': DoBreak(L);
  1147.               else Other;
  1148.              end;
  1149.           end;
  1150.        end;
  1151.        {--------------------------------------------------------------}
  1152.  
  1153.  
  1154.        Again,  notice  that  all Block does with the label is to pass it
  1155.        into DoIf and  DoBreak.    The  loop  constructs  don't  need it,
  1156.        because they are going to pass their own label anyway.
  1157.  
  1158.        The new version of DoIf is:AUAU
  1159.  
  1160. APAP
  1161.  
  1162.                                     - 20 -A*A*
  1163. PA A
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.        {--------------------------------------------------------------}
  1170.        { Recognize and Translate an IF Construct }
  1171.  
  1172.        procedure Block(L: string); Forward;
  1173.  
  1174.  
  1175.        procedure DoIf(L: string);
  1176.        var L1, L2: string;
  1177.        begin
  1178.           Match('i');
  1179.           Condition;
  1180.           L1 := NewLabel;
  1181.           L2 := L1;
  1182.           EmitLn('BEQ ' + L1);
  1183.           Block(L);
  1184.           if Look = 'l' then begin
  1185.              Match('l');
  1186.              L2 := NewLabel;
  1187.              EmitLn('BRA ' + L2);
  1188.              PostLabel(L1);
  1189.              Block(L);
  1190.           end;
  1191.           Match('e');
  1192.           PostLabel(L2);
  1193.        end;
  1194.        {--------------------------------------------------------------}
  1195.  
  1196.  
  1197.        Here,  the  only  thing  that  changes  is  the addition  of  the
  1198.        parameter to procedure Block.  An IF statement doesn't change the
  1199.        loop  nesting level, so DoIf just passes the  label  along.    No
  1200.        matter how many levels of IF nesting we have, the same label will
  1201.        be used.
  1202.  
  1203.        Now, remember that DoProgram also calls Block, so it now needs to
  1204.        pass it a label.  An  attempt  to  exit the outermost block is an
  1205.        error, so DoProgram  passes  a  null  label  which  is  caught by
  1206.        DoBreak:
  1207.  
  1208.  
  1209.        {--------------------------------------------------------------}
  1210.        { Recognize and Translate a BREAK }
  1211.  
  1212.        procedure DoBreak(L: string);
  1213.        begin
  1214.           Match('b');
  1215.           if L <> '' then
  1216.              EmitLn('BRA ' + L)
  1217.           else Abort('No loop to break from');
  1218.        end;A-A-
  1219.  
  1220.                                     - 21 -A*A*
  1221. PA A
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.        {--------------------------------------------------------------}
  1228.  
  1229.        { Parse and Translate a Program }
  1230.  
  1231.        procedure DoProgram;
  1232.        begin
  1233.           Block('');
  1234.           if Look <> 'e' then Expected('End');
  1235.           EmitLn('END')
  1236.        end;
  1237.        {--------------------------------------------------------------}
  1238.  
  1239.  
  1240.        That  ALMOST takes care of everything.  Give it a try, see if you
  1241.        can "break" it <pun>.  Careful, though.  By this time  we've used
  1242.        so many letters, it's hard to think of characters that aren't now
  1243.        representing  reserved  words.    Remember:  before  you  try the
  1244.        program, you're going to have to edit every occurence of Block in
  1245.        the other loop constructs to include the new parameter.    Do  it
  1246.        just like I did for LOOP.
  1247.  
  1248.        I  said ALMOST above.  There is one slight problem: if you take a
  1249.        hard  look  at  the code generated for DO, you'll see that if you
  1250.        break  out  of  this loop, the value of the loop counter is still
  1251.        left on the stack.  We're going to have to fix that!  A shame ...
  1252.        that was one  of  our  smaller  routines, but it can't be helped.
  1253.        Here's a version that doesn't have the problem:
  1254.  
  1255.  
  1256.        {--------------------------------------------------------------}
  1257.        { Parse and Translate a DO Statement }
  1258.  
  1259.        procedure Dodo;
  1260.        var L1, L2: string;
  1261.        begin
  1262.           Match('d');
  1263.           L1 := NewLabel;
  1264.           L2 := NewLabel;
  1265.           Expression;
  1266.           EmitLn('SUBQ #1,D0');
  1267.           PostLabel(L1);
  1268.           EmitLn('MOVE D0,-(SP)');
  1269.           Block(L2);
  1270.           EmitLn('MOVE (SP)+,D0');
  1271.           EmitLn('DBRA D0,' + L1);
  1272.           EmitLn('SUBQ #2,SP');
  1273.           PostLabel(L2);
  1274.           EmitLn('ADDQ #2,SP');
  1275.        end;
  1276.        {--------------------------------------------------------------}
  1277.  
  1278.  
  1279.        The  two  extra  instructions,  the  SUBQ and ADDQ, take care  of
  1280.        leaving the stack in the right shape.A*A*
  1281.                                     - 22 -
  1282. PA A
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.        CONCLUSION
  1289.  
  1290.        At this point we have created a number of control  constructs ...
  1291.        a richer set, really, than that provided by almost any other pro-
  1292.        gramming language.  And,  except  for the FOR loop, it was pretty
  1293.        easy to do.  Even that one was tricky only because it's tricky in
  1294.        assembler language.
  1295.  
  1296.        I'll conclude this session here.  To wrap the thing up with a red
  1297.        ribbon, we really  should  have  a  go  at  having  real keywords
  1298.        instead of these mickey-mouse  single-character  things.   You've
  1299.        already seen that  the  extension to multi-character words is not
  1300.        difficult, but in this case it will make a big difference  in the
  1301.        appearance of our input code.  I'll save that little bit  for the
  1302.        next installment.  In that installment we'll also address Boolean
  1303.        expressions, so we can get rid of the dummy version  of Condition
  1304.        that we've used here.  See you then.
  1305.  
  1306.        For reference purposes, here is  the  completed  parser  for this
  1307.        session:
  1308.  
  1309.  
  1310.        {--------------------------------------------------------------}
  1311.        program Branch;
  1312.  
  1313.        {--------------------------------------------------------------}
  1314.        { Constant Declarations }
  1315.  
  1316.        const TAB = ^I;
  1317.              CR  = ^M;
  1318.  
  1319.  
  1320.        {--------------------------------------------------------------}
  1321.        { Variable Declarations }
  1322.  
  1323.        var Look  : char;              { Lookahead Character }
  1324.            Lcount: integer;           { Label Counter }
  1325.  
  1326.  
  1327.        {--------------------------------------------------------------}
  1328.        { Read New Character From Input Stream }
  1329.  
  1330.        procedure GetChar;
  1331.        begin
  1332.           Read(Look);
  1333.        end;AEAE
  1334.  
  1335.                                     - 23 -A*A*
  1336. PA A
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.        {--------------------------------------------------------------}
  1343.        { Report an Error }
  1344.  
  1345.        procedure Error(s: string);
  1346.        begin
  1347.           WriteLn;
  1348.           WriteLn(^G, 'Error: ', s, '.');
  1349.        end;
  1350.  
  1351.  
  1352.        {--------------------------------------------------------------}
  1353.        { Report Error and Halt }
  1354.  
  1355.        procedure Abort(s: string);
  1356.        begin
  1357.           Error(s);
  1358.           Halt;
  1359.        end;
  1360.  
  1361.  
  1362.        {--------------------------------------------------------------}
  1363.        { Report What Was Expected }
  1364.  
  1365.        procedure Expected(s: string);
  1366.        begin
  1367.           Abort(s + ' Expected');
  1368.        end;
  1369.  
  1370.        {--------------------------------------------------------------}
  1371.        { Match a Specific Input Character }
  1372.  
  1373.        procedure Match(x: char);
  1374.        begin
  1375.           if Look = x then GetChar
  1376.           else Expected('''' + x + '''');
  1377.        end;
  1378.  
  1379.  
  1380.        {--------------------------------------------------------------}
  1381.        { Recognize an Alpha Character }
  1382.  
  1383.        function IsAlpha(c: char): boolean;
  1384.        begin
  1385.           IsAlpha := UpCase(c) in ['A'..'Z'];
  1386.        end;
  1387.  
  1388.  
  1389.        {--------------------------------------------------------------}
  1390.        { Recognize a Decimal Digit }
  1391.  
  1392.        function IsDigit(c: char): boolean;
  1393.        begin
  1394.           IsDigit := c in ['0'..'9'];
  1395.        end;A*A*
  1396.                                     - 24 -
  1397. PA A
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.        {--------------------------------------------------------------}
  1404.        { Recognize an Addop }
  1405.  
  1406.        function IsAddop(c: char): boolean;
  1407.        begin
  1408.           IsAddop := c in ['+', '-'];
  1409.        end;
  1410.  
  1411.  
  1412.        {--------------------------------------------------------------}
  1413.        { Recognize White Space }
  1414.  
  1415.        function IsWhite(c: char): boolean;
  1416.        begin
  1417.           IsWhite := c in [' ', TAB];
  1418.        end;
  1419.  
  1420.  
  1421.        {--------------------------------------------------------------}
  1422.        { Skip Over Leading White Space }
  1423.  
  1424.        procedure SkipWhite;
  1425.        begin
  1426.           while IsWhite(Look) do
  1427.              GetChar;
  1428.        end;
  1429.  
  1430.  
  1431.        {--------------------------------------------------------------}
  1432.        { Get an Identifier }
  1433.  
  1434.        function GetName: char;
  1435.        begin
  1436.           if not IsAlpha(Look) then Expected('Name');
  1437.           GetName := UpCase(Look);
  1438.           GetChar;
  1439.        end;
  1440.  
  1441.  
  1442.        {--------------------------------------------------------------}
  1443.        { Get a Number }
  1444.  
  1445.        function GetNum: char;
  1446.        begin
  1447.           if not IsDigit(Look) then Expected('Integer');
  1448.           GetNum := Look;
  1449.           GetChar;
  1450.        end;A9A9
  1451.  
  1452.                                     - 25 -A*A*
  1453. PA A
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.        {--------------------------------------------------------------}
  1460.        { Generate a Unique Label }
  1461.  
  1462.        function NewLabel: string;
  1463.        var S: string;
  1464.        begin
  1465.           Str(LCount, S);
  1466.           NewLabel := 'L' + S;
  1467.           Inc(LCount);
  1468.        end;
  1469.  
  1470.  
  1471.        {--------------------------------------------------------------}
  1472.        { Post a Label To Output }
  1473.  
  1474.        procedure PostLabel(L: string);
  1475.        begin
  1476.           WriteLn(L, ':');
  1477.        end;
  1478.  
  1479.  
  1480.        {--------------------------------------------------------------}
  1481.        { Output a String with Tab }
  1482.  
  1483.        procedure Emit(s: string);
  1484.        begin
  1485.           Write(TAB, s);
  1486.        end;
  1487.  
  1488.  
  1489.        {--------------------------------------------------------------}
  1490.  
  1491.        { Output a String with Tab and CRLF }
  1492.  
  1493.        procedure EmitLn(s: string);
  1494.        begin
  1495.           Emit(s);
  1496.           WriteLn;
  1497.        end;
  1498.  
  1499.  
  1500.        {--------------------------------------------------------------}
  1501.        { Parse and Translate a Boolean Condition }
  1502.  
  1503.        procedure Condition;
  1504.        begin
  1505.           EmitLn('<condition>');
  1506.        end;A9A9
  1507.  
  1508.                                     - 26 -A*A*
  1509. PA A
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.        {--------------------------------------------------------------}
  1516.        { Parse and Translate a Math Expression }
  1517.  
  1518.        procedure Expression;
  1519.        begin
  1520.           EmitLn('<expr>');
  1521.        end;
  1522.  
  1523.  
  1524.        {--------------------------------------------------------------}
  1525.        { Recognize and Translate an IF Construct }
  1526.  
  1527.        procedure Block(L: string); Forward;
  1528.  
  1529.  
  1530.        procedure DoIf(L: string);
  1531.        var L1, L2: string;
  1532.        begin
  1533.           Match('i');
  1534.           Condition;
  1535.           L1 := NewLabel;
  1536.           L2 := L1;
  1537.           EmitLn('BEQ ' + L1);
  1538.           Block(L);
  1539.           if Look = 'l' then begin
  1540.              Match('l');
  1541.              L2 := NewLabel;
  1542.              EmitLn('BRA ' + L2);
  1543.              PostLabel(L1);
  1544.              Block(L);
  1545.           end;
  1546.           Match('e');
  1547.           PostLabel(L2);
  1548.        end;
  1549.  
  1550.  
  1551.        {--------------------------------------------------------------}
  1552.        { Parse and Translate a WHILE Statement }
  1553.  
  1554.        procedure DoWhile;
  1555.        var L1, L2: string;
  1556.        begin
  1557.           Match('w');
  1558.           L1 := NewLabel;
  1559.           L2 := NewLabel;
  1560.           PostLabel(L1);
  1561.           Condition;
  1562.           EmitLn('BEQ ' + L2);
  1563.           Block(L2);
  1564.           Match('e');
  1565.           EmitLn('BRA ' + L1);
  1566.           PostLabel(L2);
  1567.        end;A6A6
  1568.                                     - 27 -A*A*
  1569. PA A
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.        {--------------------------------------------------------------}
  1576.        { Parse and Translate a LOOP Statement }
  1577.  
  1578.        procedure DoLoop;
  1579.        var L1, L2: string;
  1580.        begin
  1581.           Match('p');
  1582.           L1 := NewLabel;
  1583.           L2 := NewLabel;
  1584.           PostLabel(L1);
  1585.           Block(L2);
  1586.           Match('e');
  1587.           EmitLn('BRA ' + L1);
  1588.           PostLabel(L2);
  1589.        end;
  1590.  
  1591.  
  1592.        {--------------------------------------------------------------}
  1593.        { Parse and Translate a REPEAT Statement }
  1594.  
  1595.        procedure DoRepeat;
  1596.        var L1, L2: string;
  1597.        begin
  1598.           Match('r');
  1599.           L1 := NewLabel;
  1600.           L2 := NewLabel;
  1601.           PostLabel(L1);
  1602.           Block(L2);
  1603.           Match('u');
  1604.           Condition;
  1605.           EmitLn('BEQ ' + L1);
  1606.           PostLabel(L2);
  1607.        end;
  1608.  
  1609.  
  1610.        {--------------------------------------------------------------}
  1611.        { Parse and Translate a FOR Statement }
  1612.  
  1613.        procedure DoFor;
  1614.        var L1, L2: string;
  1615.            Name: char;
  1616.        begin
  1617.           Match('f');
  1618.           L1 := NewLabel;
  1619.           L2 := NewLabel;
  1620.           Name := GetName;
  1621.           Match('=');
  1622.           Expression;
  1623.           EmitLn('SUBQ #1,D0');
  1624.           EmitLn('LEA ' + Name + '(PC),A0');
  1625.           EmitLn('MOVE D0,(A0)');
  1626.           Expression;
  1627.           EmitLn('MOVE D0,-(SP)');
  1628.           PostLabel(L1);A*A*
  1629.                                     - 28 -
  1630. PA A
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.           EmitLn('LEA ' + Name + '(PC),A0');
  1637.           EmitLn('MOVE (A0),D0');
  1638.           EmitLn('ADDQ #1,D0');
  1639.           EmitLn('MOVE D0,(A0)');
  1640.           EmitLn('CMP (SP),D0');
  1641.           EmitLn('BGT ' + L2);
  1642.           Block(L2);
  1643.           Match('e');
  1644.           EmitLn('BRA ' + L1);
  1645.           PostLabel(L2);
  1646.           EmitLn('ADDQ #2,SP');
  1647.        end;
  1648.  
  1649.  
  1650.        {--------------------------------------------------------------}
  1651.        { Parse and Translate a DO Statement }
  1652.  
  1653.        procedure Dodo;
  1654.        var L1, L2: string;
  1655.        begin
  1656.           Match('d');
  1657.           L1 := NewLabel;
  1658.           L2 := NewLabel;
  1659.           Expression;
  1660.           EmitLn('SUBQ #1,D0');
  1661.           PostLabel(L1);
  1662.           EmitLn('MOVE D0,-(SP)');
  1663.           Block(L2);
  1664.           EmitLn('MOVE (SP)+,D0');
  1665.           EmitLn('DBRA D0,' + L1);
  1666.           EmitLn('SUBQ #2,SP');
  1667.           PostLabel(L2);
  1668.           EmitLn('ADDQ #2,SP');
  1669.        end;
  1670.  
  1671.  
  1672.        {--------------------------------------------------------------}
  1673.        { Recognize and Translate a BREAK }
  1674.  
  1675.        procedure DoBreak(L: string);
  1676.        begin
  1677.           Match('b');
  1678.           EmitLn('BRA ' + L);
  1679.        end;
  1680.  
  1681.  
  1682.        {--------------------------------------------------------------}
  1683.        { Recognize and Translate an "Other" }
  1684.  
  1685.        procedure Other;
  1686.        begin
  1687.           EmitLn(GetName);
  1688.        end;A6A6
  1689.                                     - 29 -A*A*
  1690. PA A
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.        {--------------------------------------------------------------}
  1697.        { Recognize and Translate a Statement Block }
  1698.  
  1699.        procedure Block(L: string);
  1700.        begin
  1701.           while not(Look in ['e', 'l', 'u']) do begin
  1702.              case Look of
  1703.               'i': DoIf(L);
  1704.               'w': DoWhile;
  1705.               'p': DoLoop;
  1706.               'r': DoRepeat;
  1707.               'f': DoFor;
  1708.               'd': DoDo;
  1709.               'b': DoBreak(L);
  1710.               else Other;
  1711.              end;
  1712.           end;
  1713.        end;
  1714.  
  1715.  
  1716.        {--------------------------------------------------------------}
  1717.  
  1718.        { Parse and Translate a Program }
  1719.  
  1720.        procedure DoProgram;
  1721.        begin
  1722.           Block('');
  1723.           if Look <> 'e' then Expected('End');
  1724.           EmitLn('END')
  1725.        end;
  1726.  
  1727.  
  1728.        {--------------------------------------------------------------}
  1729.  
  1730.        { Initialize }
  1731.  
  1732.        procedure Init;
  1733.        begin
  1734.           LCount := 0;
  1735.           GetChar;
  1736.        end;
  1737.  
  1738.  
  1739.        {--------------------------------------------------------------}
  1740.        { Main Program }
  1741.  
  1742.        begin
  1743.           Init;
  1744.           DoProgram;
  1745.        end.
  1746.        {--------------------------------------------------------------}ANAN
  1747.                                     - 30 -A*A*
  1748. PA A
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.        *****************************************************************
  1755.        *                                                               *
  1756.        *                        COPYRIGHT NOTICE                       *
  1757.        *                                                               *
  1758.        *   Copyright (C) 1988 Jack W. Crenshaw. All rights reserved.   *
  1759.        *                                                               *
  1760.        *****************************************************************AUAU
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766. A0A0
  1767.  
  1768.                                     - 31 -A*A*
  1769. @